//
//  HBBankListCtrl.m
//  HuoBao
//
//  Created by chaiyanan on 15/11/27.
//  Copyright © 2015年 jinxin. All rights reserved.
//

#import "HBBankListCtrl.h"
#import "MJRefresh.h"
#import "HBBankListCell.h"
#import "HBGetBankListInfo.h"
#import "HBGetBankListItemInfo.h"
#import "HBGetBankListParam.h"
#import "HBGetBankListParser.h"


@interface HBBankListCtrl ()
<
UITableViewDataSource,
UITableViewDelegate
>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) HBGetBankListInfo *bankListInfo;

@end

@implementation HBBankListCtrl

#pragma mark - system

- (void)viewDidLoad
{
    [super viewDidLoad];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:YES];
}

- (void)buildLayout
{
    [super buildLayout];
    self.title = @"银行卡类型";
    _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-64)];
    [self.view addSubview:_tableView];
    INIT_TABLE(_tableView);
    [self requestToGetBankList];
    
}

#pragma mark - private

#pragma mark - active

#pragma mark - delegate&datasource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.bankListInfo.dataAry.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *cellIdentifier = NSStringFromClass([HBBankListCell class]);
    HBBankListCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (!cell)
    {
        cell = [[HBBankListCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    HBGetBankListItemInfo *itemInfo = self.bankListInfo.dataAry[indexPath.row];
    cell.headImageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"%@",itemInfo.bankCode]];
    cell.nameLable.text = itemInfo.bankName;
    cell.desLable.text = itemInfo.bankLimit;
    
    [cell reloadViewType];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 1/[UIScreen mainScreen].scale;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 1/[UIScreen mainScreen].scale)];
    view.backgroundColor = [UIColor clearColor];
    return view;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return Height_HBBankListCell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    if (self.bankBlock) {
        HBGetBankListItemInfo *itemInfo = self.bankListInfo.dataAry[indexPath.row];
        self.bankBlock(itemInfo.bankName,itemInfo.bankCode,self.userName);
    }
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - net

- (void)requestToGetBankList
{
    [CustomLoadingView showHudToView:self.view];
    HBGetBankListParam *param = [[HBGetBankListParam alloc]init];
    param.uid = [HBLoginManager sharedHBLoginManager].currentUserInfo.uid;
    param.token = [HBLoginManager sharedHBLoginManager].currentUserInfo.token;
    HBGetBankListParser *parser = [[HBGetBankListParser alloc]init];
    [NetCenter getDataFromURL:Method_getBankList params:param parser:parser result:^(ParserResult *myResult) {
        [CustomLoadingView hideHudForView:self.view];
        if (myResult.code==0)
        {
            HBGetBankListInfo *listInfo = (HBGetBankListInfo *)myResult.result;
            self.bankListInfo = listInfo;
            [self.tableView reloadData];
        }
        else
        {
            DISSMISS_ERR(myResult.message);
        }
        
    } failure:^(NSError *error) {
        [CustomLoadingView hideHudForView:self.view];
        DISSMISS_ERR([error description]);
    }];
}



@end
